# -*- coding: utf-8 -*-
'''
@File  : jar-deploy.py
@Author: canx
@Date  : 2018/8/10 16:37
@Desc  : bhge 项目部署
'''

import os
import sshForLinux


projectWinPath = "D:\\softCode\\ge-code\\bhge"
javaMvnCmd = "mvn -DskipTests=true package"

projectLinuxPath = "/home/app/bhge"

ip = "47.98.156.138"
username = "app"  #用户名
passwd = "Techm#002"    #密码

local = "%s\\target\\bhge-0.0.1-SNAPSHOT.jar"%(projectWinPath)
remote = "%s/bhge-0.0.1-SNAPSHOT.jar"%(projectLinuxPath)

currentProfileCmd = "source /home/app/.bash_profile && "

# step 1 java project package
os.chdir(projectWinPath)
print("step 1 project package")
# system 成功返回0，否则返回1
result = os.system(javaMvnCmd)
print(result)


# step 2 stop jar service stop && backup jar
#你要执行的命令列表
print("step 2 service stop and backup jar")
stopAndBackUpCmd = ['sh %s/stop.sh'%(projectLinuxPath), 'mv %s/bhge-0.0.1-SNAPSHOT.jar %s/bhge-0.0.1-SNAPSHOT.jar.bak'%(projectLinuxPath, projectLinuxPath)]
sshForLinux.ssh2_cmd(ip, username, passwd, stopAndBackUpCmd)

# step 3 sftp the jar to linux
print("step 3 file put")
sshForLinux.sftp_upload(ip, username, passwd, local, remote)


# step 4  jar service start
print("step 4 service start")
startCmd = ['%s sh %s/start.sh'%(currentProfileCmd, projectLinuxPath), 'echo done!']
print(startCmd)
sshForLinux.ssh2_cmd(ip, username, passwd, startCmd)